﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rep="urn:report-extended-utilities"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl rep">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="report-name">SharePoint Generic Report</xsl:param>
  <xsl:param name="report-chart-type">Bar</xsl:param>
  <xsl:param name="report-date" />
  <xsl:param name="report-topN">10</xsl:param>
  <xsl:template match="/">
    <xsl:variable name="report-legend-title">Site collections</xsl:variable>
    <Report>
      <xsl:attribute name="Name">
        <xsl:value-of select="$report-name"/>
      </xsl:attribute>
      <xsl:attribute name="ChartType">
        <xsl:value-of select="$report-chart-type"/>
      </xsl:attribute>
      <xsl:attribute name="LegendTitle">
        <xsl:value-of select="$report-legend-title"/>
      </xsl:attribute>
      <Filters>
        <Filter Name="All web applications - Storage" FilterAttribute="Usage" FilterValue="Storage" />
        <Filter Name="All web applications - Visits" FilterAttribute="Usage" FilterValue="Visits" />
        <Filter Name="All web applications - Hits" FilterAttribute="Usage" FilterValue="Hits" />
        <Filter Name="All web applications - Bandwidth" FilterAttribute="Usage" FilterValue="Bandwidth" />
        <xsl:for-each select="//WebApplications/WebApplication">
          <Filter Name="{@Url}" FilterAttribute="WebApplication" FilterValue="{@Id}" />
        </xsl:for-each>
      </Filters>
      <Series>
        <Serie Name="Visits - Top {$report-topN}">
          <xsl:for-each select="//Sites/Site">
            <xsl:sort order="descending" select="Usage/@Visits"/>
            <xsl:if test="position() &lt;= $report-topN">
              <xsl:variable name="WebAppId">
                <xsl:value-of select="../../@Id"/>
              </xsl:variable>
              <xsl:variable name="UsageVisits" select="Usage/@Visits"/>
              <Item Usage="Visits">
                <xsl:attribute name="Name">
                  <xsl:value-of select="@Url"/>
                </xsl:attribute>
                <xsl:attribute name="Value">
                  <xsl:value-of select="$UsageVisits"/>
                </xsl:attribute>
                <xsl:attribute name="WebApplication">
                  <xsl:value-of select="$WebAppId" />
                </xsl:attribute>
                <xsl:attribute name="Date">
                  <xsl:value-of select="$report-date"/>
                </xsl:attribute>
              </Item>
            </xsl:if>
          </xsl:for-each>
        </Serie>
        <Serie Name="Bandwidth - Top {$report-topN}">
          <xsl:for-each select="//Sites/Site">
            <xsl:sort order="descending" select="Usage/@Bandwidth"/>
            <xsl:if test="position() &lt;= $report-topN">
              <xsl:variable name="WebAppId">
                <xsl:value-of select="../../@Id"/>
              </xsl:variable>
              <xsl:variable name="UsageBandwidth" select="Usage/@Bandwidth"/>
              <Item Usage="Bandwidth">
                <xsl:attribute name="Name">
                  <xsl:value-of select="@Url"/>
                </xsl:attribute>
                <xsl:attribute name="Value">
                  <xsl:value-of select="$UsageBandwidth"/>
                </xsl:attribute>
                <xsl:attribute name="WebApplication">
                  <xsl:value-of select="$WebAppId" />
                </xsl:attribute>
                <xsl:attribute name="Date">
                  <xsl:value-of select="$report-date"/>
                </xsl:attribute>
              </Item>
            </xsl:if>
          </xsl:for-each>
        </Serie>
        <Serie Name="Storage - Top {$report-topN}">
          <xsl:for-each select="//Sites/Site">
            <xsl:sort order="descending" select="Usage/@Storage"/>
            <xsl:if test="position() &lt;= $report-topN">
              <xsl:variable name="WebAppId">
                <xsl:value-of select="../../@Id"/>
              </xsl:variable>
              <xsl:variable name="UsageStorage" select="round(Usage/@Storage div 1048576 * 100) div 100"/>
              <Item Usage="Storage">
                <xsl:attribute name="Name">
                  <xsl:value-of select="@Url"/>
                </xsl:attribute>
                <xsl:attribute name="Value">
                  <xsl:value-of select="$UsageStorage"/>
                </xsl:attribute>
                <xsl:attribute name="WebApplication">
                  <xsl:value-of select="$WebAppId" />
                </xsl:attribute>
                <xsl:attribute name="Date">
                  <xsl:value-of select="$report-date"/>
                </xsl:attribute>
              </Item>
            </xsl:if>
          </xsl:for-each>
        </Serie>
        <Serie Name="Hits - Top {$report-topN}">
          <xsl:for-each select="//Sites/Site">
            <xsl:sort order="descending" select="Usage/@Hits"/>
            <xsl:if test="position() &lt;= $report-topN">
              <xsl:variable name="WebAppId">
                <xsl:value-of select="../../@Id"/>
              </xsl:variable>
              <xsl:variable name="UsageHits" select="Usage/@Hits"/>
              <Item Usage="Hits">
                <xsl:attribute name="Name">
                  <xsl:value-of select="@Url"/>
                </xsl:attribute>
                <xsl:attribute name="Value">
                  <xsl:value-of select="$UsageHits"/>
                </xsl:attribute>
                <xsl:attribute name="WebApplication">
                  <xsl:value-of select="$WebAppId" />
                </xsl:attribute>
                <xsl:attribute name="Date">
                  <xsl:value-of select="$report-date"/>
                </xsl:attribute>
              </Item>
            </xsl:if>
          </xsl:for-each>
        </Serie>
      </Series>
    </Report>
  </xsl:template>
</xsl:stylesheet>
